package com.ruoyi.web.controller.workflow;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.workflow.domain.ServiceItem;
import com.ruoyi.workflow.service.ICarPackageAuditService;
import com.ruoyi.workflow.service.IServiceItemService;
import com.ruoyi.workflow.service.ITodoTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/workflow/task")
public class TodoTaskController extends BaseController {
    @Autowired
    private ITodoTaskService todoTaskService;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IServiceItemService serviceItemService;

    /**
     * 查询待办任务
     * @param params 根据日期时间搜索
     * @return
     */
    @PreAuthorize("@ss.hasPermi('workflow:task:todo')")
    @GetMapping("/todo")
    public TableDataInfo todo(@RequestParam Map<String,Object> params){
        return todoTaskService.todo(params);
    }

    /**
     * 审批任务
     * @param taskId 任务节点ID
     * @param approvalStatus 审批结果
     * @param info 批注信息
     * @return
     */
    @PreAuthorize("@ss.hasPermi('workflow:task:audit')")
    @PostMapping("/audit")
    public AjaxResult approvalTask(String taskId,Integer approvalStatus,String info){
        //接受不到前端传过来的值
        todoTaskService.approvalTask(taskId,approvalStatus,info);
        return AjaxResult.success("审核任务成功");
    }

    /**
     * 修改申请表单
     * @param serviceItem 封装申请的数据
     * @param auditId 套餐信息的ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('workflow:task:update')")
    @PutMapping("/update")
    public AjaxResult updateForm(ServiceItem serviceItem,Long auditId){
        //更新套餐信息
        carPackageAuditService.updateFormCarPackageAudit(serviceItem,auditId);
        //更新服务项
        serviceItemService.updateFormServiceItem(serviceItem);
        return AjaxResult.success("修改成功");
    }

    /**
     * 重新发起申请
     * @param taskId 当前任务节点ID
     * @param packageAuditInfo 发起申请信息
     * @return
     */
    @PreAuthorize("@ss.hasPermi('workflow:task:reApply')")
    @PostMapping("/reApply")
    public AjaxResult reApplyPackageAudit(String taskId,String packageAuditInfo){
        todoTaskService.reApplyPackageAudit(taskId,packageAuditInfo);
        return AjaxResult.success("成功重新发起申请");
    }
}
